@tool
class_name Guard_EntityDataAttribute_Number
extends Guard
enum CalcType
{
	Less,
	LessEqual,
	Equal,
	NotEqual,
	GreaterEqual,
	Greater,
}

@export var attribute_name:String
@export var target_value:CompositeProperty
@export var calc_type:CalcType
## 对于target_value，负数是否表示无限大
@export var negative_number_is_inf:bool = false

func is_satisfied(transition:Transition,state:State) -> bool:
	var entity:Entity = state.chart.get_expression_property("Entity")
	if not entity or not entity.entity_data:
		return false
	var attr:Attribute = entity.entity_data.find_attribute(attribute_name)
	if not attr:
		return false
	if typeof(attr.get_value()) in [TYPE_INT,TYPE_FLOAT]:
		return _calc_by_calc_type(attr.get_value(),target_value.get_value(entity),calc_type)
	elif typeof(attr.get_value()) in [TYPE_VECTOR2,TYPE_VECTOR2I]:
		return _calc_by_calc_type(attr.get_value().length(),target_value.get_value(entity),calc_type)
	return false

func _calc_by_calc_type(value,target_value,calc_type:CalcType) -> bool:
	match calc_type:
		CalcType.Less:
			return value < target_value or (negative_number_is_inf and target_value < 0)
		CalcType.LessEqual:
			return value <= target_value or (negative_number_is_inf and target_value < 0)
		CalcType.Equal:
			return value == target_value or (negative_number_is_inf and value < 0 and target_value < 0)
		CalcType.NotEqual:
			return value != target_value
		CalcType.GreaterEqual:
			return (not negative_number_is_inf and value >= target_value) or (negative_number_is_inf and target_value >= 0 and value >= target_value)
		CalcType.Greater:
			return (not negative_number_is_inf and value >= target_value) or (negative_number_is_inf and target_value >= 0 and value >= target_value)
		_:
			return false

func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
